'''类的封装'''
class People:
    __brithplace="地球"
    def __init__(self,Id,name,age,gendler,salary):
        self.Id=Id
        self.name=name
        self.age=age
        self.gendler=gendler
        self.sarlary=salary
    def getId(self):
        print("私有方法、找到%s的Id是%s" %(self.name,self.Id))
p1=People("7698758767676","萌萌",21,"女","8989")
print(People.__dict__)
print(p1._People__brithplace)
# 1.类的普通封装s
# 2.定义内部属、只限类内部使用、会被重新重新命名

class Room:

    def __init__(self,name,ower,length,width,heigh):
        self.name = name
        self.ower = ower
        self.__lenght = length
        self.__width = width
        self.__heigh = heigh
        print("类内部使用私有属性",self.__heigh)
    def tell_area(self):
        return self.__lenght*self.__width
    def tell_volume(self):
        return self.__lenght*self.__width*self.__heigh

r1 =Room("别墅","萌萌",124,234,288)
print("外部通过类提供的接口访问类部私有属性：",r1.tell_volume())


'''实例对象自省函数 ,x 为实例名、y为字符串(实例的数字属性、类的函数属性)
hasattr(x,"y")
getattr(x,"y")
setattr(x,"y")
delattr(x,"y")
'''


hasattr(r1,'name')
print(hasattr(r1,'name'))

setattr(r1,'sb',True)
setattr(r1,'SSSB',True)
print(r1.__dict__)  # >>>'sb': True, 相当于r1.sh='True'

setattr(r1,"func",lambda x:x+1)

x=r1.func(11)
x=r1.func(11)
print("添加函数后调用的结果",x)

setattr(r1,"func1",lambda self:self.ower+'sb')
print("添加func1后的结果",r1.func1(r1))

print("添加两个函数之后的结果",r1.__dict__)
delattr(r1,'SSSB')  # del r1.SSSB
print('删除SSB的结果',r1.__dict__)
getattr(r1,'sb') # 查r1对象中是否存在sb的属性、存在时返回结果、否则报错
getattr(r1,'sb','存在r1.sb这个属性')  # 查r1对象中是否存在sb的属性、返回结果
print(getattr(r1,'sb','存在r1.sb这个属性'))  # >>>结果为True

